<?php
include 'system/app.php';
//include 'system/class/db/mysqli.php';
//include 'system/class/frontaction.php';

$dbConf = array (
    'driver' => 'mysql',
    'host' => 'localhost',
    'port' => '6033',
    'database' => 'db_mydinner',
    'database' => 'db_sudingke',
    'username' => 'root',
    'password' => 'root',
    'charset' => 'utf8',
    'throw_exceptions' => true
);

function pre(array $o){
    echo '<pre>';
}

//App::loadClass('action_front');

///////////////////////////////
if(preg_match('/^\/(admin|company)\//', $_SERVER['REDIRECT_URL'])){
    @list($tmp, $tmp1, $act, $mod, $param) = explode('/', $_SERVER['REDIRECT_URL'], 5);
    $act = empty($act) ? 'default' : $act;
    $mod = empty($mod) ? 'index' : $mod;
    $base = 'admin';
    define('IN_ADMIN', true);
}else{
    @list($tmp, $act, $mod, $param) = explode('/', $_SERVER['REDIRECT_URL'], 4);
    $act = empty($act) ? 'default' : $act;
    $mod = empty($mod) ? 'index' : $mod;
    $base = 'front';
}

$action = App::action($act, $base);
$method = 'on'.$mod;
if(method_exists($action, $method)){
    call_user_func(array($action, $method));
}elseif(method_exists($action, 'onDefault')){
    call_user_func(array($action, 'onDefault'));
}else{
    exit('The method `' .$mod. '` is not exists!');
}
exit;

/*
echo '<hr>';
$db = new Db_Mysqli($dbConf);
$db->reconnect();
//$data = $db->proc('call new_routine ()');
//print_r($data);


$stmt = $db->prepare('delete from `biz_shop` where shop_id = ?');
$stmt->bind_param('i', $shopid);
foreach(array(1,2,3) as $shopid){
    $stmt->execute();
}
echo $db->execute('delete from `biz_shop` where shop_id <=35');
*/

echo '<hr>';
Db::instance($dbConf);

test01();
test02();
test03();

echo '<hr />Running Time (seconds):';
echo microtime(true) - SYS_START_TIME;




function output($line){
    print_r($line);
    echo "\n";
}

function test01(){
    output('<h1>Test01</h1>');

    $data = array(
        'name' => "myshop - ".rand(10000, 999999),
    );

    $ret = Db::insert('biz_shop', $data);
    output('Insert Id = '.$ret);

    $ret = Db::execute('select * from `biz_shop`  ');
    output('Afetched Rows = '.$ret);

    $ret = Db::execute('delete from `biz_shop` where shop_id = '.$ret);
    output('Afetched Rows = '.$ret);
}

function test02(){
    output('<h1>Test02</h1>');

    $data = Db::proc('call new_routine()');
    output($data);
}

function test03(){
    output('<h1>Test03</h1>');

    for($i=0; $i<5; $i++){
        $data[] = array(
            'name' => "New Shop: ".rand(10000, 999999),
        );
    }

    $ret = Db::insertBatch('biz_shop', $data);
    output($ret);
}
?>